<template>
	<view>
		<view class="order-list_item">
			<slot name="orderNo"></slot>
			<view class="order-list_main mb20" v-if='isListShow'
				v-for="(item,index) in innerData[valueProps.productDtlList]" :key="item.id">
				<view class="order-list_main_img">
					<image class="order-img" lazy-load :src="item.imagePath" mode="aspectFill"></image>
				</view>
				<view class="order-list_main_right">
					<view class="order-list_main_name">
						<view>{{item.productName}}</view>
						<view class="main-price">￥{{item.marketPrice}}</view>
					</view>
					<view class="order-list_main_number">
						<view>{{item.specPro}}</view>
						<view class="main-number">
							<text class="iconfont icon-chahao"></text>{{item.amount}}
						</view>
					</view>
					<view class="order-list_main_tag">
						七天内发货
					</view>
				</view>
			</view>
			<!-- 详情，确认订单模块 -->
			<view class="order-list_main" v-if='!isListShow'>
				<view class="order-list_main_img">
					<image class="order-img" lazy-load :src="innerData[valueProps.thumb]" mode="aspectFill"></image>
				</view>
				<view class="order-list_main_right">
					<view class="order-list_main_name">
						<view>{{innerData[valueProps.name]}}</view>
						<view class="main-price">￥{{innerData[valueProps.price]}}</view>
					</view>
					<view class="order-list_main_number">
						<view>{{innerData[valueProps.biaozhun]}}</view>
						<view class="main-number">
							<text class="iconfont icon-chahao"></text>{{innerData[valueProps.orderqty]}}
						</view>
					</view>
					<view class="order-list_main_tag">
						七天内发货
					</view>
				</view>
			</view>
			<view>
				<slot name="dealerName"></slot>
				<slot name="orderTime"></slot>
				<view class="order-list_footer" v-if="numberShow&&show">
					<view>购买数量</view>
					<view class="order-list_footer_text footer-number-box">
						<u-number-box v-model="innerData[valueProps.orderqty]" @change="onChange" :disabledInput="true"
							:min="1" :button-size="22">
						</u-number-box>
					</view>
				</view>
				<view v-if="show">
					<!-- 可以输入赠品明细 经销商新增，业务员修改-->
					<view class="order-list_footer flex-between" v-if="isPropGift&&giftReadonly==2">
						<view>赠品、样品及其他：
							<br><view class="" style="color: #999999;font-size: 24rpx;">{{innerData[valueProps.zengpinMessage]||''}}</view>
						</view>
						<view @click="openif=true" v-if="userRole!='2-1'&&userRole!='2-2'">
							增加批注<text class="iconfont icon-arrow-right-line"></text>
						</view>
						<view class="" v-else @click="isZengpin=true">
							<text class="iconfont icon-arrow-right-line"></text>
						</view>
					</view>
					<!-- 赠品明细只读 -->
					<view class="order-list_footer" v-if="!isPropGift&&giftReadonly==1">
						<view>赠品、样品及其他：</view>
						<view class="order-list_footer_text">{{innerData[valueProps.zengpinMessage]||''}}</view>
					</view>
					<!-- 可以输入备注 -->
					<view class="order-list_footer flex-between" v-if="isPropMark" @click="isRemark=true">
						<view>备注：</view>
						<text class="order-list_footer_text">{{innerData[valueProps.orderMark]}}</text>
						<text class="iconfont icon-arrow-right-line"></text>
					</view>
					<!-- 备注只读 -->
					<view class="order-list_footer" v-if="!isPropMark">
						<view>备注：<text style="color: #999999;font-size: 24rpx;">{{innerData[valueProps.orderMark]||''}}</text></view>
					</view>
					<view class="order-list_footer">
						<view class="order-list_footer_total">共{{innerData[valueProps.totalNum]}}件，<text
								class="total_text">共计:</text><text
								class="total_price">￥{{innerData[valueProps.totalPrice]||0}}</text></view>
					</view>
				</view>
			</view>
		</view>
		<view class="" v-if="popup">
			<u-popup :show="isZengpin" @close="isZengpin=false" :round="30">
				<view class="popup-order-remark">
					<text class="popup-order-remark_text">赠品、样品及其他</text>
					<view class="mt10 popup-order-remark_textarea">
						<u--textarea v-model="innerData[valueProps.zengpinMessage]" placeholder="选填" height="120"
							border="none">
						</u--textarea>
					</view>
				</view>
				<u-button type="primary" @click="giftHandle()">确定</u-button>
			</u-popup>
			<u-popup :show="isRemark" @close="isRemark=false" :round="30">
				<view class="popup-order-remark">
					<text class="popup-order-remark_text">订单备注</text>
					<view class="mt10 popup-order-remark_textarea">
						<u--textarea v-model="innerData[valueProps.orderMark]" placeholder="选填" height="120" border="none" count>
						</u--textarea>
					</view>
				</view>
				<u-button type="primary" @click="remarkHandle()">确定</u-button>
			</u-popup>
			<u-popup :show="openif" @close="openif=false" :round="30">
				<view class="popup-order-remark">
					<text class="popup-order-remark_text">增加订单批注</text>
					<view class="mt10 popup-order-remark_textarea" style="margin: 20rpx;">
						<u--textarea v-model="innerData[valueProps.orderComment]" placeholder="必填" height="120">
						</u--textarea>
					</view>
				</view>
				<u-button type="primary" @click="giftRemarkHandle()" >保存批注</u-button>
			</u-popup>
		</view>
	</view>
</template>

<script>
	export default {
		name: "su-order-card", //订单组件
		props: {
			isListShow: { //是否是列表订单显示
				type: Boolean,
				default: false
			},
			show: { //是否显示底部订单信息
				type: Boolean,
				default: true
			},
			numberShow: { //是否显示订单数量
				type: Boolean,
				default: true
			},
			data: { //订单信息
				type: Object,
				default: function() {
					return {}
				}
			},
			isPropGift: { //是否可以输入赠品
				type: Boolean,
				default: true
			},
			giftReadonly: { //区分是详情还是确认订单跳转
				type: Number,
				default: 2
			},
			isPropMark: { //是否可以输入备注
				type: Boolean,
				default: true
			},
			valueProps: {
				type: Object,
				default: function() {
					return {
						id: "id", // 主键字段名
						name: "name",
						thumb: "thumb",
						price: "price", // 实际显示的标注字段名
						biaozhun: "biaozhun",
						orderqty: "orderqty", //购买数量
						zengpinMessage: "gift",
						orderComment:"giftRemark",
						orderMark: "remark",
						totalNum: "totalNum", //总件数
						totalPrice: 'totalPrice', //总计
					};
				}
			}
		},
		data() {
			return {
				openif:false,
				popup: false,
				innerData: {},
				orderData: this.data,
				valueProp: this.valueProps,
				orderNum: 0, //单个商品数量
				orderPriceNum: '', //单个商品总价
				isRemark: false,
				isZengpin: false,
				userRole: this.$store.state.userRole //角色
			};
		},
		mounted() {
			console.log('当前角色',this.userRole)
			this.innerData = this.data;
			if(!this.isListShow){
				this.popup = true			
			}
		},
		watch: {
			//处理弹窗被fixed遮挡问题
			isRemark(val) {
				this.$store.commit('setPopupOpen', val)
			},
			isZengpin(val) {
				this.$store.commit('setPopupOpen', val)
			},
			openif(val) {
				this.$store.commit('setPopupOpen', val)
			},
		},
		methods: {
			//明细修改
			giftHandle() {
				console.log(this.innerData[this.valueProps.id])
				this.isZengpin = false
				console.log('赠品信息', this.innerData[this.valueProps.zengpinMessage])
				const data = {
					dtlId: this.innerData[this.valueProps.id],
					gift: this.innerData[this.valueProps.zengpinMessage],
				}
				this.$emit('giftUpdate', this.innerData)
			},
			
			//备注修改
			remarkHandle() {
				this.isRemark = false
				const data = {
					remark: this.innerData[this.valueProps.orderMark]
				}
				this.$emit('markEdit', this.innerData)
			},
			// 增加批注
			giftRemarkHandle(){
				
				this.openif = false
				const data = {
					dtlId: this.innerData[this.valueProps.id],
					giftRemark: this.innerData[this.valueProps.orderComment]
				}
				this.$emit('addcomment', data)
			},
			onChange(e) {
				console.log(e)
				const totalNum = this.valueProp.totalNum
				const orderqty = this.valueProp.orderqty
				const totalPrice = this.valueProp.totalPrice
				const price = this.valueProp.price
				this.innerData[totalNum] = e.value; //单品总数量
				console.log('总数量', this.innerData[totalNum]);
				this.innerData[totalPrice] = this.innerData[price] * this.innerData[totalNum] //单品总价
				this.$emit('change', this.innerData)
				this.$emit('input', this.innerData)
			},
		}
	}
</script>

<style lang="scss">
	.order-list_item {
		background: #fff;
		padding: 30upx 40upx;
		margin-top: 20upx;

		.order-list_main {
			display: flex;

			&_img {
				height: 124upx;
				background-color: #f7f7f7;
				border-radius: 10upx;
				width: 106upx;

				.order-img {
					width: 100%;
					height: 100%;
					border-radius: 10upx;
				}
			}

			&_tag {
				display: inline-block;
				padding: 4upx 18upx;
				background-image: -webkit-linear-gradient(right, #E94F4E, #F4A668);
				-webkit-background-clip: text;
				-webkit-text-fill-color: transparent;
				font-size: 24upx;
				margin-top: 15upx;
				border: 1px solid #F4A668;
				border-radius: 15upx;
			}

			&_right {
				flex: 1;
				margin-left: 20upx;
			}

			&_name {
				font-size: 32upx;
				color: #333;
				display: flex;
				font-weight: bold;

				.main-price {
					text-align: right;
					flex: 1;
				}
			}

			&_number {
				font-size: 26upx;
				color: #999;
				display: flex;
				padding-top: 15upx;

				.main-number {
					text-align: right;
					flex: 1;

					.icon-chahao {
						font-size: 20upx;
						font-weight: 100;
					}
				}

			}
		}
	}

	.check-list_remark {
		/deep/.u-cell__body {
			padding: 0;
			color: #333;

		}
	}

	.order-list_footer {
		display: flex;
		font-size: 28upx;
		padding-top: 40upx;

		&_text {
			flex: 1;
			text-align: right;
			overflow: hidden;
			white-space: nowrap;
			text-overflow: ellipsis;
			margin-left: 40upx;
		}

		&_total {
			flex: 1;
			text-align: right;
			color: #999;
			font-size: 26upx;

			.total_text {
				font-weight: bold;
				color: #333;
			}

			.total_price {
				color: #EB1515;
				font-weight: bold;
			}
		}

		.footer-number-box {
			position: absolute;
			right: 0;
			margin-right: 50upx;

			/deep/.u-number-box {
				.u-number-box__input {
					background: #fff !important;
				}

				.u-number-box__minus {
					width: 50upx !important;
				}

				.u-number-box__plus {
					width: 50upx !important;
				}

				span {
					font-size: 24upx !important;
				}
			}
		}
	}

	.popup-order-remark {
		padding: 20upx 10upx 200upx;

		&_textarea {
			/deep/.u-textarea {
				background-color: #f7f7f7 !important;

			}
		}

		&_text {
			font-size: 28upx;
			color: #333;
			font-weight: bold;
			text-align: center;
			display: block;
		}
	}
</style>
